﻿using aier.business.logic;
using aier.dataaccess.model;
using aier.dataaccess.dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
  
    [RoutePrefix("api/Street")]
    public class StreetController : ApiController
    {
        public StreetBll streetInfoBll { get; set; }

        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 根据省市区名称获取街道信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        //[Route("api/Street/GetAllStreet")]
        [ActionName("GetStreetByPCA")]
        public IHttpActionResult GetStreetByPCA(string provinceName, string cityName, string areaName)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            List<StreetView> streets = streetInfoBll.QueryStreetByProviceCityArea(provinceName, cityName, areaName,"");
 
            return Ok<List<StreetView>>(streets);
        }

        /// <summary>
        /// 通过ID获取街道信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[Route("api/Street/GetStreetById")]
        [ActionName("GetStreetById")]
        public IHttpActionResult GetStreetById(string streetId)
        {
            StreetView StreetView = streetInfoBll.QueryStreetById(streetId);

            return Ok<StreetView>(StreetView);
        }

        /// <summary>
        /// 通过名称获取街道信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetStreetByName")]
        public IHttpActionResult GetStreetByName(string streetName, string provinceName, string cityName, string areaName)
        {
            try {

                List<StreetView> StreetViews = streetInfoBll.QueryStreetByName(streetName, provinceName, cityName, areaName);

                return Ok<List<StreetView>>(StreetViews);
            } catch (Exception ex) {
                return new ApiErrorResult(ex.Message, Request);
            }
           
        }

        /// <summary>
        /// 通过名称获取街道信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetStreetByName")]
        public IHttpActionResult GetStreetByProviceCityCounty([FromUri]string provice, [FromUri]string city, [FromUri]string area)
        {
            List<StreetView> StreetViews = streetInfoBll.QueryStreetByProviceCityCounty(provice,  city,  area);

            return Ok<List<StreetView>>(StreetViews);
        }

        /// <summary>
        /// 根据区县查所属街道 
        /// 原程序 LocationQueryServiceImpl/queryStreet
        /// </summary>
        /// <param name="areaid">区县ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryStreetByAreaId")]
        public IHttpActionResult QueryStreetByAreaId(Guid areaid)
        {
            List<StreetView> streetViews = streetInfoBll.QueryStreetByAreaId(areaid);
            return Ok<List<StreetView>>(streetViews);
        }
        /// <summary>
        /// 根据不同条件查询街道数量,只要属性不为空就视为查询条件
        /// 查询条件为：省份ID/市ID/区ID/街道ID/名称
        /// 原程序 LocationQueryServiceImpl/queryStreetCount
        /// </summary>
        /// <param name="streetView">街道对象</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("QueryStreetCount")]
        public IHttpActionResult QueryStreetCount(StreetView  streetView)
        {
            int count = streetInfoBll.QueryStreetCount(streetView);
            return Ok<int>(count);
        }
        /// <summary>
        /// 创建街道
        /// </summary>
        /// <param name="streetJson"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IHttpActionResult Put(Street streetInfo)
        {
            try {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                streetInfo.ownerId = userInfo.userid;
                return Ok<Guid>(streetInfoBll.CreateStreet(streetInfo));
            } catch (Exception ex) {
                return new ApiErrorResult(ex.Message, Request);
            }
           
        }

      
        /// <summary>
        /// 修改街道
        /// </summary>
        /// <param name="streetJson"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IHttpActionResult Post(Street streetInfo)
        {
            try {
                if(ModelState.IsValid)
                {
                    streetInfoBll.UpdateStreetById(streetInfo);
                    return Ok();
                }
                
                return BadRequest(ModelState);
               
            } catch (Exception ex) {
                return new ApiErrorResult(ex.Message, Request);
            }
           
          
        }
    }
}
